package qianfeng.com.meijiadaka.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import qianfeng.com.meijiadaka.R;
import qianfeng.com.meijiadaka.bean.detail.CommodityBean;

public class CommodityAdapter extends BaseAdapter{

    private Context context;
    private CommodityBean cb;
    private List<Object> list;

    public CommodityAdapter(Context context, CommodityBean cb) {
        this.context = context;
        this.cb = cb;
        list = new ArrayList<>();
        if (cb.getResult().getRelatedList().size()>0){
            list.add(cb.getResult().getRelatedList());
        }
       list.add(cb.getResult().getDeliveryStatus());
        Log.e( "CommodityAdapter:list.size() ", list.size()+"");

    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).equals(cb.getResult().getRelatedList())&&cb.getResult().getRelatedList().size()>0){
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public int getViewTypeCount() {
        return list.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ViewHolder1 viewHolder1 = null;
        int type = getItemViewType(position);
        Log.e("getView: ","1111111111" );
        if (convertView==null){
            switch (type){
                case 0:  //相关产品
                    convertView = LayoutInflater.from(context).inflate(R.layout.commodity_detail2_correlation_product,parent,false);
                    viewHolder = new ViewHolder();
                    viewHolder.rv = (RecyclerView) convertView.findViewById(R.id.commodity_detail_recyclerview);
                    convertView.setTag(viewHolder);
                    break;
                case 1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.commodity_detail3_imagetext_details,parent,false);
                    viewHolder1 = new ViewHolder1();
                    viewHolder1.wv = (WebView) convertView.findViewById(R.id.webView);
                    convertView.setTag(viewHolder1);
                    break;
            }

        }else{
            switch (type){
                case 0:
                    viewHolder = (ViewHolder) convertView.getTag();
                    break;
                case 1:
                    viewHolder1 = (ViewHolder1) convertView.getTag();
                    break;
            }
        }

        Log.e("getView: type======",type+"" );
        //赋值
        switch (type){
            case 0:
                if (list.get(position).equals(cb.getResult().getRelatedList())&&cb.getResult().getRelatedList().size()>0){
                    CommodityCorrelationProductRecyclerAdapter adapter = new
                            CommodityCorrelationProductRecyclerAdapter(cb,context);

                    LinearLayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
                    viewHolder.rv.setLayoutManager(manager);
                    viewHolder.rv.setAdapter(adapter);
                    adapter.setOnItemClickListener(new CommodityCorrelationProductRecyclerAdapter.OnItemClickListener() {
                        @Override
                        public void onClick(int index) {
                            Toast.makeText(context, "点击"+index , Toast.LENGTH_SHORT).show();
//                            Intent intent = new Intent(context, SpeciaDetailsActivity.class);
//                            intent.putExtra("id",st.get(index).getTopicId());
//                            context.startActivity(intent);
                        }
                    });
                }
                break;
            case 1:
                viewHolder1.wv.setWebViewClient(new WebViewClient());
                viewHolder1.wv.setWebChromeClient(new WebChromeClient());
                //加载数据
                viewHolder1.wv.loadDataWithBaseURL(null,cb.getResult().getDescPictures(),"text/html", "UTF-8", null);
                break;
                }

        return convertView;
    }


    class ViewHolder{
        RecyclerView rv;
    }
    class ViewHolder1{
        WebView wv;
    }


}
